package com.jrwd.recycletest;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jrwd.recycletest.util.CacheUtil;

import butterknife.ButterKnife;

/**
 * 相信自己，创造未来
 * Created by 小兵 on 2017/9/11.
 */

public abstract class BaseAct extends AppCompatActivity {
    public Toolbar toolbar;
    public Context mContext;
    public CacheUtil cacheUtil;
    public TextView tv_right;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mContext=this;
        cacheUtil =CacheUtil.get(mContext);
        setContentView(setContentView(savedInstanceState));
        ButterKnife.bind(this);
        initView();
    }

    public abstract int setContentView(Bundle savedInstanceState);

    public abstract void initView();

    public void setToolbar(int logo, String title) {
        if (toolbar == null) {
            toolbar = (Toolbar) findViewById(R.id.app_toolbar);
        }
        if (logo != 0) {
            toolbar.setNavigationIcon(logo);
        }else {
            toolbar.setNavigationIcon(null);
        }
        if (!TextUtils.isEmpty(title)) {
            TextView tv = (TextView) findViewById(R.id.toolbar_title);
            tv.setText(title);
        }
        toolbar.setTitle("");
//        setSupportActionBar(toolbar);
    }
    public void setRightText(String text, final onToolBarListner mlistener){
        if(tv_right==null) {
            tv_right = (TextView) findViewById(R.id.tv_right);

        }
        tv_right.setText(text);
        if(mlistener!=null){
            tv_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mlistener.onRight();
                }
            });
        }
    }



    public void toast(String s){
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
    }
    public void toast(int s){
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();

    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    public interface onToolBarListner{

        public void onRight();
    }

}

